package com.etonenet.test.hazelcast.nearcache;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.MapEvent;
import com.hazelcast.map.listener.EntryAddedListener;
import com.hazelcast.map.listener.EntryEvictedListener;
import com.hazelcast.map.listener.EntryRemovedListener;
import com.hazelcast.map.listener.EntryUpdatedListener;
import com.hazelcast.map.listener.MapClearedListener;
import com.hazelcast.map.listener.MapEvictedListener;

/**
 * 在客户端使用near Cache
 * <p>
 * <a href=
 * "http://docs.hazelcast.org/docs/3.5/manual/html-single/index.html#client-near-cache-configuration">
 * client-near-cache-configuration</a>
 * 
 * @author wxu
 *
 */
public class Client {

	private final static Logger logger = LoggerFactory.getLogger(Client.class);

	public static void main(String[] args) {
		new Client();
	}

	private IMap<String, String> nearCacheMap;

	public Client() {
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setProperty("hazelcast.logging.type", "slf4j");
		clientConfig.getGroupConfig().setName(Server.GROUP_NAME).setPassword(Server.GROUP_PASSWORD);

		//@formatter:off
		
		//@formatter:on

		HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
		nearCacheMap = client.getMap("nearCacheMap");
		nearCacheMap.addEntryListener(new MyEntryListener(), true);

		reader.run();

	}

	Runnable reader = new Runnable() {
		public void run() {
			while (true) {
				// 因为没有使用nearcache，所以每一次get，都会从server获取（server的lastAccessTime会因此出现变更）
				String value = nearCacheMap.get("test");
				logger.info("get value:{}", value);

				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}
	};

	/**
	 * map listener
	 * 
	 * @author wxu
	 *
	 */
	static class MyEntryListener implements EntryAddedListener<String, String>, EntryUpdatedListener<String, String>,
			EntryRemovedListener<String, String>, EntryEvictedListener<String, String>, MapEvictedListener,
			MapClearedListener {
		@Override
		public void entryAdded(EntryEvent<String, String> event) {
			System.out.println("entryAdded:" + event);
		}

		@Override
		public void entryRemoved(EntryEvent<String, String> event) {
			System.out.println("entryRemoved:" + event);
		}

		@Override
		public void entryUpdated(EntryEvent<String, String> event) {
			System.out.println("entryUpdated:" + event);
		}

		@Override
		public void entryEvicted(EntryEvent<String, String> event) {
			System.out.println("entryEvicted:" + event);
		}

		@Override
		public void mapEvicted(MapEvent event) {
			System.out.println("mapEvicted:" + event);
		}

		@Override
		public void mapCleared(MapEvent event) {
			System.out.println("mapCleared:" + event);

		}
	}

}
